package hu.dynamicwebshop.gui;

import com.vaadin.server.Sizeable.Unit;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;
import hu.dynamicwebshop.daos.impls.MenuDAOImpl;
import hu.dynamicwebshop.entities.Menu;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 *
 * @author Szecskó Zoltán <zoltan.szecsko@gmail.com>
 */
public class MenuLayer extends HorizontalLayout
        implements ApplicationContextAware {

    private static final long serialVersionUID = 1L;
    private String width = "900";
    private MenuBar menu = new MenuBar();
    private List<Menu> lastSavedMenuList;
    private MenuDAOImpl mdi;
    private ApplicationContext ctx;

    public MenuLayer() {
        setStyleName("menuLayer");
        setWidth(width);
        addMenuStructure();
    }

    //hozzáadja a MenuBar -t a felülethez
    private void addMenuStructure() {
        menu.setWidth(100.0f, Unit.PERCENTAGE);
        menu.setAutoOpen(true);
        addComponent(menu);
    }

    //betölti a menüt az adatbázisból
    public void loadMenu() {
        Comparator<Menu> comparator = new Comparator<Menu>() {

            @Override
            public int compare(Menu o1, Menu o2) {
                return o1.getPlaceId() - o2.getPlaceId();
            }

        };

        mdi = ((MenuDAOImpl) ctx.getBean("menuDaoImpl"));
        lastSavedMenuList = new ArrayList<Menu>();
        lastSavedMenuList = mdi.getMenuStructure();

        if (!lastSavedMenuList.isEmpty()) {
            //beáálítjuk a menüpontok sorrendjét

            java.util.Collections.sort(lastSavedMenuList, comparator);

            for (Menu m : lastSavedMenuList) {
                //menu.addItem(m.getMenuName(), null);
                if (m.getParentId() == 0) {
                    menu.addItem(m.getMenuName(), null);
                }

            }
        }
    }

    //elmenti a hozzáadott menüpontokat
    public void saveMenu() {
        List<MenuItem> menuItems = menu.getItems();

        //még nem volt menü mentve az adatbázisba
        if (lastSavedMenuList.isEmpty()) {
            int placeId = 1;

            for (MenuItem m : menuItems) {
                System.out.println("ciklus");
                Menu menu = new Menu();
                System.out.println(m.getText());
                menu.setPlaceId(placeId);
                //vizsgálni kell, hogy van-e szülője vagy sem
                //ha van szülője, akkor ki kell kerestetni az ID-ját
                if (m.getParent() != null) {
                    menu.setParentId(mdi.getMenuIdByName(m.getParent().getText()));
                } else {
                    //ha nincs szülője, akkor a parentId nulla
                    menu.setParentId(0);
                }

                menu.setMenuName(m.getText());

                mdi.saveMenu(menu);
                //ide mentjük az utolsó mentett állapotot
                lastSavedMenuList = mdi.getMenuStructure();
            }
        } else {//már volt mentve menü az adatbázisba
            for (MenuItem actualMenu : menuItems) {
                if (needToSave(actualMenu)) {
                    Menu menu = new Menu();
                    int maxPlaceId = ((MenuDAOImpl) ctx.getBean("menuDaoImpl")).getLastPlaceId();
                    menu.setPlaceId(maxPlaceId + 1);
                    if (actualMenu.getParent() != null) {
                        menu.setParentId(mdi.getMenuIdByName(actualMenu.getParent().getText()));
                    } else {
                        //ha nincs szülője, akkor a parentId nulla
                        menu.setParentId(0);
                    }
                    menu.setMenuName(actualMenu.getText());

                    mdi.saveMenu(menu);
                    lastSavedMenuList = mdi.getMenuStructure();
                }
            }
        }
    }

    private boolean needToSave(MenuItem actualMenu) {
        boolean needSave = true;
        for (Menu m2 : lastSavedMenuList) {
            if (m2.getMenuName().equals(actualMenu.getText())) {
                needSave = false;
            }
        }

        return needSave;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
        loadMenu();
    }

    public MenuBar getMenu() {
        return menu;
    }

    public void refreshMenuBar() {
        menu.removeItems();
        loadMenu();
    }

    public void placeIdWarning(String message) {
        Window warningWindow = new Window();
        Label label = new Label(message);
        HorizontalLayout layout = new HorizontalLayout();
        layout.addComponent(label);
        warningWindow.setContent(layout);
        UI.getCurrent().addWindow(warningWindow);
    }
}
